**File: partisanship_regressions.do
**Description: Perform individual level voter file Analysis found in Fraga, Moskowitz and Schneer
**Due to data use permissions, we are not able to post the indidividual level catalist voter file data used in this code

clear all

cap cd "~Desktop/replication"

set more off
set matsize 10000

use "analysis_file_2pct_2016", clear

*determine reg status
gen reg_status = 1 if ca_partyaffiliation == "DEM"
replace reg_status = -1 if ca_partyaffiliation == "REP"
replace reg_status = 0 if (ca_partyaffiliation!="" & reg_status==.) | (ca_partyregstate=="Y" & ca_partyaffiliation=="" & reg_status==.)
gen _pvi_reg_status = _pvi*reg_status

*drop unregistered voters
drop if reg_status == 0 | reg_status==.

*id competitive/uncompetitive districts
gen district_comp = -1 if (50+_pvi)<=45
replace district_comp = 1 if (50+_pvi) >=55
replace district_comp = 0 if _pvi!=. & district_comp==.

*id first period cd
gen _cd_pre = _cd_ if year==2008
bysort anid (year): replace _cd_pre = _cd_pre[1]

*Create treatment data; "treated units" have partisanship that matches their district

gen treat = reg_status == district_comp
bysort anid (year): gen ever_treat = (treat[2] == 1 | treat[3]==1)

gen treat2 = 1 if reg_status == district_comp
replace treat2 = -1 if -reg_status == district_comp
replace treat2 = 0 if treat2==. & reg_status!=district_comp

gen treat_unmatch = (treat2== -1)
bysort anid (year): gen ever_treat_unmatch = (treat_unmatch[2] == 1 | treat_unmatch[3]==1)

*Create conditions to filter data

*All voters who are registered
gen dropcond = (reg_status==. | reg_status == 0 )

bysort anid (year): egen dropcond0 = total(dropcond)
replace dropcond = (dropcond0>0)
drop dropcond0

*Filter out competitive districts
gen dropcond1 = (district_comp == 0) | reg_status==. | reg_status == 0 

bysort anid (year): egen dropcond0 = total(dropcond1)
replace dropcond1 = (dropcond0>0)
drop dropcond0

*Filter out competitive districts and districts that match in period 1; misalignment to align/misalign

gen dropcond2 = (district_comp == 0) | reg_status==. | reg_status == 0 | (district_comp==reg_status & year==2008)

bysort anid (year): egen dropcond0 = total(dropcond2)
replace dropcond2 = (dropcond0>0)
drop dropcond0

*Filter out competitive districts and districts that don't match in period 1; alignment to align/misalign

gen dropcond2b = (district_comp == 0) | reg_status==. | reg_status == 0 | (district_comp!=reg_status & year==2008)

bysort anid (year): egen dropcond0 = total(dropcond2b)
replace dropcond2b = (dropcond0>0)
drop dropcond0

*Filter out districts that do match and districts that don't match in first period; competitiveness to competitive or partisan aligned
gen dropcond4 = (reg_status==. | reg_status == 0 | (year<=2010 & treat2 !=0) | treat2== (-1))

bysort anid (year): egen dropcond0 = total(dropcond4)
replace dropcond4 = (dropcond0>0)
drop dropcond0

*Filter out districts that do match and districts that don't match in first period; competitiveness to misaligned or partisan aligned
gen dropcond4b = (reg_status==. | reg_status == 0 | (year<=2010 & treat2 !=0) | (year>=2012 & treat2==0))

bysort anid (year): egen dropcond0 = total(dropcond4b)
replace dropcond4b = (dropcond0>0)
drop dropcond0

*Filter out districts that aren't competitive in first period; filter out districts that are partisan match in second period; competitive to competitive or mismatch 
gen dropcond6 = (reg_status==. | reg_status == 0 | (year<=2010 & treat2 !=0) | treat2==1)

bysort anid (year): egen dropcond0 = total(dropcond6)
replace dropcond6 = (dropcond0>0)
drop dropcond0

*Create matching groups

gen redist_period = (year>=2012)

egen g0 = group(_state_n _cd_pre)
tempfile d_all d_temp

save `d_all', replace
save `d_temp', replace

local a = 1 

foreach var of varlist bp_abs_pvi_sr2__all bp_abs_pvi_sr2_08_12 bp_abs_pvi_sr2_10_14 bp_abs_pvi_sr2_08_16 {

local b = 1
foreach var2 of varlist dropcond*{

use `d_all', clear
keep if `var'==1 & `var2'==0
count

tab g0
local gmax = r(r)
di "`b'"

bysort anid redist_period (year): gen g1 = (_n==1)
if `b'>=6 | `b'==4{

cem g0 (#`gmax') if g1==1, treatment(ever_treat_unmatch)
	foreach varb of varlist cem*{
		bysort anid redist_period (year): replace `varb' = `varb'[1]
	}
} 
else {

cem g0 (#`gmax') if g1==1, treatment(ever_treat)
foreach varb of varlist cem*{
		bysort anid redist_period (year): replace `varb' = `varb'[1]
	}
}
tab year treat [aweight=cem_weights], m

foreach var3 of varlist cem*{
rename `var3' `var3'_`a'_`b'
}
local b = `b' + 1

keep anid year cem*
merge 1:1 anid year using `d_temp'
tab _merge
drop _merge
save `d_temp', replace
}
local a = `a' + 1

}

use `d_temp', clear

drop g0

order cem*, last

label var _voted "Turnout"
label var _pvi_reg_status "Strength of Partisan Alignment"
label var treat `"\shortstack{Partisan \\ Aligned}"'
label var treat2 `"\shortstack{Partisan \\ Aligned}"'
label var treat_unmatch `"\shortstack{Partisan \\ Misaligned}"'


//Block Fixed Effects Approach

rename (anid anid_txt) (anid_n anid)

merge m:1 anid using "data/voter_history_1996_2014_2pct"
drop if _merge==2
drop _merge

**create age groups

gen age_group = 1 if age<25
replace age_group = 2 if age>=25 & age < 35
replace age_group = 3 if age>=35 & age < 45
replace age_group = 4 if age>=45 & age < 55
replace age_group = 5 if age>=55 & age < 65
replace age_group = 6 if age >=65

label define age_lbl 1 "18-24" 2 "25-34" 3 "35-44" 4 "45-54" 5 "55-64" 6 "65+"

label values age_group age_lbl

//create blocks that group individuals
bysort anid (year): gen _cd_n_yr1 = _cd_n[1]

egen block = group(reg_status black hispanic asian female age_group _cd_n_yr1 _voted2008 _voted2010) if year == 2012, missing

bysort anid (year): replace block = block[3] if missing(block) & (year >=2014)

rename (anid anid_n) (anid_txt anid)

egen _st_yr_party_n = group(state_n year reg_status)

tempfile analysis_file_2016_temp

save `analysis_file_2016_temp', replace

*Merge in District  (time-varying) characteristics

use "district_turnout_characteristics", clear

rename (st_abbrev district) (state_name _cd_)

foreach var of varlist hh_med_inc hh_mean_inc pct_nh* pct_hispanic inc house_topticket{

rename `var' cd_`var'
}

tempfile dist_characteristics

save `dist_characteristics', replace


use `analysis_file_2016_temp', clear

merge m:1 state_name _cd_ year using `dist_characteristics'
drop if _merge==2
drop _merge


// Restrict to party registration states


keep if inlist(state_name,"AK","AZ","CA","CO","CT","DC","DE","FL","IA") | inlist(state_name,"KS","KY","MA","MD","ME","NC","NE","NH") /// 
| inlist(state_name,"NJ","NM","NV","NY","OK","OR","PA","RI","SD") | inlist(state_name,"UT","WV","WY")

tab year treat [aweight=cem_weights_1_3], m
tab year treat [aweight=cem_weights_1_4], m
tab year treat [aweight=cem_weights_1_5], m // competitive to comp/aligned
tab year treat [aweight=cem_weights_1_7], m // competitive to comp/misaligned


//Analysis section

drop _cd_pre_post _cd_pre_post_n
bysort anid (year): gen _cd_pre_post = string(_cd_[1]) + "_" + string(_cd_[3]) + "_" + state_name + "_" + string(reg_status)
egen _cd_pre_post_n = group(_cd_pre_post)

egen _cd_pre_n = group(_cd_pre state_name reg_status)

*Identify "battleground states" for later
gen battleground = (inlist(state_name,"CO","FL","IA","NV","NC","OH","VA","WI"))

save `analysis_file_2016_temp', replace


set more off
set matsize 10000

local note1 ""

local st1 ""
local st2 "noredist"

local felab1 "State-Year FEs"

local fe1 = "_st_yr_n"

*Unweighted Regressions -- Including all districts, whether redistricted or not
*All Districts

local allarestrict1 = "& bp_abs_pvi_sr2__all==1 & dropcond2==0"
local allarestrict2 = "& bp_abs_pvi_sr2__all==1 & dropcond2b==0"
local allarestrict3 = "& bp_abs_pvi_sr2__all==1 & dropcond4==0"
local allarestrict4 = "& bp_abs_pvi_sr2__all==1 & dropcond6==0"

*Mid term Years

local midarestrict1 = "& bp_abs_pvi_sr2_10_14==1 & dropcond2==0"
local midarestrict2 = "& bp_abs_pvi_sr2_10_14==1 & dropcond2b==0"
local midarestrict3 = "& bp_abs_pvi_sr2_10_14==1 & dropcond4==0"
local midarestrict4 = "& bp_abs_pvi_sr2_10_14==1 & dropcond6==0"

*Pres Years

local prsarestrict1 = "& bp_abs_pvi_sr2_08_12==1 & dropcond2==0"
local prsarestrict2 = "& bp_abs_pvi_sr2_08_12==1 & dropcond2b==0"
local prsarestrict3 = "& bp_abs_pvi_sr2_08_12==1 & dropcond4==0"
local prsarestrict4 = "& bp_abs_pvi_sr2_08_12==1 & dropcond6==0"

local prsa16restrict1 = "& bp_abs_pvi_sr2_08_16==1 & dropcond2==0"
local prsa16restrict2 = "& bp_abs_pvi_sr2_08_16==1 & dropcond2b==0"
local prsa16restrict3 = "& bp_abs_pvi_sr2_08_16==1 & dropcond4==0"
local prsa16restrict4 = "& bp_abs_pvi_sr2_08_16==1 & dropcond6==0"


*Weighted Regressions -- I.e., w/ in redistricted distrits only
*ALl years
local allbrestrict1 = "[aweight=cem_weights_1_3]"
local allbrestrict2 = "[aweight=cem_weights_1_4]"
local allbrestrict3 = "[aweight=cem_weights_1_5]"
local allbrestrict4 = "[aweight=cem_weights_1_7]"

*Mid term Years

local midbrestrict1 = "[aweight=cem_weights_3_3]"
local midbrestrict2 = "[aweight=cem_weights_3_4]"
local midbrestrict3 = "[aweight=cem_weights_3_5]"
local midbrestrict4 = "[aweight=cem_weights_3_7]"

*Presidential Years

local prsbrestrict1 = "[aweight=cem_weights_2_3]"
local prsbrestrict2 = "[aweight=cem_weights_2_4]"
local prsbrestrict3 = "[aweight=cem_weights_2_5]"
local prsbrestrict4 = "[aweight=cem_weights_2_7]"

local prsb16restrict1 = "[aweight=cem_weights_4_3]"
local prsb16restrict2 = "[aweight=cem_weights_4_4]"
local prsb16restrict3 = "[aweight=cem_weights_4_5]"
local prsb16restrict4 = "[aweight=cem_weights_4_7]"


local titlenote1 "All Years"
local titlenote2 "Mid-Term Years"
local titlenote3 "Presidential Years (2008 \& 2012)"
local titlenote4 "Presidential Years (2008 \& 2016)"
local titlenote5 "All Years (Redistricted Districts Only)"
local titlenote6 "Mid-Term Years (Redistricted Districts Only)"
local titlenote7 "Presidential Years (2008 \& 2012, Redistricted Districts Only)"
local titlenote8 "Presidential Years (2008 \& 2016, Redistricted Districts Only)"

*Panel Data analysis

local strestrict1 ""

local i = 1

foreach item in "alla" "mida" "prsa" "prsa16" "allb" "midb" "prsb" "prsb16"{

forvalues j = 1(1)1{

forvalues n = 1(1)1{

*Block fixed effects
xtset block

forvalues k = 1(1)3{
	
		if "`item'" == "alla" {
				
				areg _voted treat i.`fe`n'' if !missing(block) & year >=2012 `strestrict`j'' ``item'restrict`k'', absorb(block) cluster(_cd_pre_n)
		} 
		else {
			areg _voted treat i.`fe`n'' if !missing(block) & year >=2012 `strestrict`j'' ``item'restrict`k'', absorb(block) cluster(_cd_pre_post_n)
		}
		
		estimates store reg`k'a
		estadd local bfe "Yes"
		estadd local styrfe "Yes"
	
}

local k = 4

if "`item'" == "alla" {
areg _voted treat_unmatch i.`fe`n'' if !missing(block) & year >=2012 `strestrict`j'' ``item'restrict`k'', absorb(block) cluster(_cd_pre_n)

		} 
		else {
			areg _voted treat_unmatch i.`fe`n'' if !missing(block) & year >=2012 `strestrict`j'' ``item'restrict`k'', absorb(block) cluster(_cd_pre_post_n)
		}


estimates store reg`k'a
estadd local bfe "Yes"
estadd local styrfe "Yes"

esttab reg1a reg2a reg3a reg4a using "out/table_`item'_`n'_bfe_2pct`st`j''.tex", order(treat) drop( *y*r* _cons) star(* 0.10 ** 0.05 *** 0.01) r2 se label compress nonotes nomtitles ///
mgroups("\shortstack{Misaligned to\\Aligned/\\Misaligned}" "\shortstack{Aligned to\\Aligned/\\Misaligned}" "\shortstack{Competitive to\\Competitive/\\Aligned}" "\shortstack{Competitive to \\Competitive/\\Misaligned}", pattern(1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
addnotes("Standard errors, clustered at the pre/post redistricting Party-CD level, are in parentheses." "* p $<$ 0.10, ** p $<$ 0.05, *** p $<$ 0.01" "The sample is comprised of contested general elections by a D and R candidate." ///
"Treatment occurs in second period." "All elections in Louisiana are excluded from the sample due to their unusual rules." `note`j'') /// *substitute({table} {sidewaystable}) ///
title("Block FE: Individual Regressions of Turnout on Partisan Alignment, Aligned versus Misaligned Districts, `titlenote`i'' with `felab`n'' \label{tab`item'bfe}") scalars("styrfe `felab`n''" "bfe Block FEs") replace

estimates clear




*Diff in Diff

xtset anid

forvalues k = 1(1)3{

xtreg _voted treat i.`fe`n'' if !missing(_cd_pre_post_n) `strestrict`j'' ``item'restrict`k'', fe cluster(_cd_pre_post_n)
estadd local ife "Yes"
estadd local styrfe "Yes"
estimates store reg`k'b
}

local k = 4

xtreg _voted treat_unmatch i.`fe`n'' if !missing(_cd_pre_post_n) `strestrict`j'' ``item'restrict`k'', fe cluster(_cd_pre_post_n)
estadd local ife "Yes"
estadd local styrfe "Yes"
estimates store reg`k'b


esttab reg1b reg2b reg3b reg4b using "out/table_`item'_`n'_2pct`st`j''.tex", order(treat) drop( *_st_yr* _cons) star(* 0.10 ** 0.05 *** 0.01) r2 se label compress nonotes nomtitles ///
mgroups("\shortstack{Misaligned to\\Aligned/\\Misaligned}" "\shortstack{Aligned to\\Aligned/\\Misaligned}" "\shortstack{Competitive to\\Competitive/\\Aligned}" "\shortstack{Competitive to\\Competitive/\\Misaligned}", ///
pattern(1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
addnotes("Standard errors, clustered at the pre/post redistricting Party-CD level, are in parentheses." "* p $<$ 0.10, ** p $<$ 0.05, *** p $<$ 0.01" "The sample is comprised of contested general elections by a D and R candidate." ///
"Treatment occurs in second period." "All elections in Louisiana are excluded from the sample due to their unusual rules." `note`j'') /// *substitute({table} {sidewaystable}) ///
title("Individual Regressions of Turnout on Partisan Alignment, Aligned versus Misaligned Districts, `titlenote`i'' with `felab`n'' \label{tab`item'did}") scalars("styrfe `felab`n''" "ife Individual FEs" ) replace
estimates clear

}
}

local i = `i' + 1
}



*Additional robustness checks
*Assess time varying characteristics as covariates		
						
local allbrestrict1 = "[aweight=cem_weights_1_3]"
local allbrestrict2 = "[aweight=cem_weights_1_4]"
local allbrestrict3 = "[aweight=cem_weights_1_5]"
local allbrestrict4 = "[aweight=cem_weights_1_7]"

forvalues k = 1(1)3{
	areg _voted treat i._st_yr_n if !missing(block) & year >=2012 & maj_min_st==0 `allbrestrict`k'', absorb(block) cluster(_cd_pre_post_n)
	estadd local bfe "Yes"
	estadd local styrfe "Yes"
	estimates store reg`k'a
	
	
	areg _voted treat cd_log_hh_med_inc cd_pct_nh_black cd_pct_hispanic cd_pct_nh_asian i._st_yr_n if !missing(block) & year >=2012 `allbrestrict`k'', absorb(block) cluster(_cd_pre_post_n)		
	estadd local bfe "Yes"
	estadd local styrfe "Yes"
	estadd local inc "Yes"
	estadd local race "Yes"
	estimates store tvcovreg`k'a

}

local k = 4

	areg _voted treat_unmatch i._st_yr_n if !missing(block) & year >=2012  & maj_min_st==0  `allbrestrict`k'', absorb(block) cluster(_cd_pre_post_n)
	estadd local bfe "Yes"
	estadd local styrfe "Yes"
	estimates store reg`k'a

	areg _voted treat_unmatch cd_log_hh_med_inc cd_pct_nh_black cd_pct_hispanic cd_pct_nh_asian i._st_yr_n if !missing(block) & year >=2012 `allbrestrict`k'', absorb(block) cluster(_cd_pre_post_n)
	estadd local bfe "Yes"
	estadd local styrfe "Yes"
	estadd local inc "Yes"
	estadd local race "Yes"
	estimates store tvcovreg`k'a

	
esttab reg1a reg2a reg3a reg4a using "out/table_maj_min_bfe_2pct.tex", order(treat) drop( *y*r* _cons) star(* 0.10 ** 0.05 *** 0.01) r2 se label compress nonotes nomtitles ///
mgroups("\shortstack{Misaligned to\\Aligned/\\Misaligned}" "\shortstack{Aligned to\\Aligned/\\Misaligned}" "\shortstack{Competitive to\\Competitive/\\Aligned}" "\shortstack{Competitive to \\Competitive/\\Misaligned}", pattern(1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
addnotes("Standard errors, clustered at the pre/post redistricting Party-CD level, are in parentheses." "* p $<$ 0.10, ** p $<$ 0.05, *** p $<$ 0.01" "The sample is comprised of contested general elections by a D and R candidate." ///
"Treatment occurs in second period." "All elections in Louisiana are excluded from the sample due to their unusual rules.") ///
title("Block FE: Individual Regressions of Turnout on Partisan Alignment, Aligned versus Misaligned Districts, All Years (Redistricted Districts Only) with State-Year FEs \label{majminbfe}") ///
scalars("styrfe State-Year FEs" "bfe Block FEs") replace

esttab tvcovreg1a tvcovreg2a tvcovreg3a tvcovreg4a using "out/table_tvcov_bfe_2pct.tex", order(treat) drop( *y*r* _cons cd_*	) star(* 0.10 ** 0.05 *** 0.01) r2 se label compress nonotes nomtitles ///
mgroups("\shortstack{Misaligned to\\Aligned/\\Misaligned}" "\shortstack{Aligned to\\Aligned/\\Misaligned}" "\shortstack{Competitive to\\Competitive/\\Aligned}" "\shortstack{Competitive to \\Competitive/\\Misaligned}", pattern(1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
addnotes("Standard errors, clustered at the pre/post redistricting Party-CD level, are in parentheses." "* p $<$ 0.10, ** p $<$ 0.05, *** p $<$ 0.01" "The sample is comprised of contested general elections by a D and R candidate." ///
"Treatment occurs in second period." "All elections in Louisiana are excluded from the sample due to their unusual rules.") ///
title("Block FE: Individual Regressions of Turnout on Partisan Alignment, Aligned versus Misaligned Districts, All Years (Redistricted Districts Only) with State-Year FEs \label{tvcovbfe}") ///
scalars("styrfe State-Year FEs" "bfe Block FEs" "inc Income Controls" "race Race Controls") replace

estimates clear


*Prepare data for generating histogram illustrating competitiveness change
keep if bp_abs_pvi_sr2__all==1 & _voted!=.
keep anid year _pvi _state_

reshape wide _pvi, i(anid _state_) j(year)

*lower numbers mean more competitive

gen diff_pvi_08_12 = _pvi2012-_pvi2008
gen diff_pvi_10_14 = _pvi2014-_pvi2010
gen diff_pvi_08_16 = _pvi2016-_pvi2008

hist diff_pvi_08_12, scheme(tufte) percent xtitle(Change in District Partisanship (D Share)) ytitle(Percent of Citizens)
graph export "out/hist_08_12.pdf", replace

hist diff_pvi_10_14, scheme(tufte) percent xtitle(Change in District Partisanship (D Share)) ytitle(Percent of Citizens)
graph export "out/hist_10_14.pdf", replace

hist diff_pvi_08_16, scheme(tufte) percent xtitle(Change in District Partisanship (D Share)) ytitle(Percent of Citizens)
graph export "out/hist_08_16.pdf", replace


*Analysis using party registration to determine partisan composition of district, rather than PVI

use `analysis_file_2016_temp', clear

*create index for marginal voters
gen marginal2_tot = 0
gen marginal2_vote = 0

foreach item of varlist _voted2008 -_voted2010{
replace marginal2_tot = marginal2_tot + 1 if !missing(`item')
replace marginal2_vote = marginal2_vote + `item' if !missing(`item')
}

gen marginal2 = marginal2_vote / marginal2_tot


gen marginal2_q = "Always Voter" if marginal2==1
replace marginal2_q = "Marginal Voter" if marginal2==.5
replace marginal2_q = "Marginal Voter" if marginal2==0

label define voters 0 "Always Voter" 1 "Marginal Voter"

gen marginal3 = (marginal2<1)

label values marginal3 voters

tab year marginal2_q if _voted==1 & bp_abs_pvi_sr2__all==1 & inlist(year,2012,2014,2016), row

label var _voted "Turnout"


*Graph marginal voters by year

graph bar (mean) _voted if bp_abs_pvi_sr2__all==1 & inlist(year,2012,2014,2016), over(year) over(marginal3) scheme(s1mono) ytitle(Turnout)

graph export "out/marg_by_year.pdf", replace

tempfile temp

save `temp', replace

gen n = 1
collapse (count) n  if _voted==1 & bp_abs_pvi_sr2__all==1 & inlist(year,2012,2014,2016), by(year marginal3)

egen N = total(n), by(year)

gen pct = n / N

graph bar (asis) pct, over(marginal3, label(angle(forty_five))) over(year) ytitle(Share of Voters)  scheme(s1mono)

graph export "out/margshare_by_year.pdf", replace

use `temp', clear

gen marg = (inlist(marginal2_q,"Marginal Voter","Never Voter"))

*Regressions for marginal voters and never voters versus always-voters
matrix fx = J(6,3,0)
matrix fx2 = J(6,3,0)

areg _voted treat##marg i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & (dropcond2==0 | dropcond4==0), absorb(block) cluster(_cd_pre_n)

lincom 1.treat + 1.treat#1.marg
matrix fx[1,1] = r(estimate)
matrix fx[1,2]=r(lb)
matrix fx[1,3]=r(ub)

lincom 1.treat#1.marg
matrix fx2[1,1] = r(estimate)
matrix fx2[1,2]=r(lb)
matrix fx2[1,3]=r(ub)

areg _voted treat_unmatch##marg i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & (dropcond2b==0 | dropcond6 == 0), absorb(block) cluster(_cd_pre_n)

lincom -1.treat_unmatch - 1.treat_unmatch#1.marg
matrix fx[2,1] = r(estimate)
matrix fx[2,2]=r(lb)
matrix fx[2,3]=r(ub)

lincom -1.treat_unmatch#1.marg
matrix fx2[2,1] = r(estimate)
matrix fx2[2,2]=r(lb)
matrix fx2[2,3]=r(ub)

areg _voted treat##marg i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond2==0, absorb(block) cluster(_cd_pre_n)

lincom 1.treat + 1.treat#1.marg
matrix fx[3,1] = r(estimate)
matrix fx[3,2]=r(lb)
matrix fx[3,3]=r(ub)

lincom 1.treat#1.marg
matrix fx2[3,1] = r(estimate)
matrix fx2[3,2]=r(lb)
matrix fx2[3,3]=r(ub)

areg _voted treat##marg i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond2b==0, absorb(block) cluster(_cd_pre_n)

lincom 1.treat + 1.treat#1.marg
matrix fx[4,1] = r(estimate)
matrix fx[4,2]=r(lb)
matrix fx[4,3]=r(ub)

lincom 1.treat#1.marg
matrix fx2[4,1] = r(estimate)
matrix fx2[4,2]=r(lb)
matrix fx2[4,3]=r(ub)

areg _voted treat##marg i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond4==0, absorb(block) cluster(_cd_pre_n)

lincom 1.treat + 1.treat#1.marg
matrix fx[5,1] = r(estimate)
matrix fx[5,2]=r(lb)
matrix fx[5,3]=r(ub)

lincom 1.treat#1.marg
matrix fx2[5,1] = r(estimate)
matrix fx2[5,2]=r(lb)
matrix fx2[5,3]=r(ub)

areg _voted treat_unmatch##marg i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond6==0, absorb(block) cluster(_cd_pre_n)

lincom -1.treat_unmatch - 1.treat_unmatch#1.marg
matrix fx[6,1] = r(estimate)
matrix fx[6,2]=r(lb)
matrix fx[6,3]=r(ub)

lincom -1.treat_unmatch#1.marg
matrix fx2[6,1] = r(estimate)
matrix fx2[6,2]=r(lb)
matrix fx2[6,3]=r(ub)

coefplot matrix(fx[,1]), ci((fx[,2] fx[,3])) xline(0) xtitle("Change in Turnout") coeflabels(r1="Misaligned/Competitive to All" r2= "Aligned/Competitive to All" r3="Misaligned to Aligned/Misaligned" r4="Aligned to Aligned/Misaligned" r5="Competitive to Competitive/Aligned" r6="Competitive to Competitive/Misaligned") scheme(s1mono)

graph export "out/fx.pdf", replace

coefplot matrix(fx2[,1]), ci((fx2[,2] fx2[,3])) xline(0) xtitle("Change in Turnout") coeflabels(r1="Misaligned/Competitive to All" r2= "Aligned/Competitive to All" r3="Misaligned to Aligned/Misaligned" r4="Aligned to Aligned/Misaligned" r5="Competitive to Competitive/Aligned" r6="Competitive to Competitive/Misaligned") scheme(s1mono)

graph export "out/fx2.pdf", replace

*Regressions examining results for midterm years versus presidential years

gen midterm = (inlist(year,2010,2014))

matrix fx3 = J(6,3,0)

areg _voted treat##midterm if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & (dropcond2==0 | dropcond4==0), absorb(block) cluster(_cd_pre_n)

lincom 1.treat#1.midterm
matrix fx3[1,1] = r(estimate)
matrix fx3[1,2]=r(lb)
matrix fx3[1,3]=r(ub)

areg _voted treat_unmatch##midterm if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & (dropcond2b==0 | dropcond6 == 0), absorb(block) cluster(_cd_pre_n)

lincom -1.treat_unmatch#1.midterm
matrix fx3[2,1] = r(estimate)
matrix fx3[2,2]=r(lb)
matrix fx3[2,3]=r(ub)

areg _voted treat##midterm if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond2==0, absorb(block) cluster(_cd_pre_n)

lincom 1.treat#1.midterm
matrix fx3[3,1] = r(estimate)
matrix fx3[3,2]=r(lb)
matrix fx3[3,3]=r(ub)

areg _voted treat##midterm if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond2b==0, absorb(block) cluster(_cd_pre_n)

lincom 1.treat#1.midterm
matrix fx3[4,1] = r(estimate)
matrix fx3[4,2]=r(lb)
matrix fx3[4,3]=r(ub)

areg _voted treat##midterm if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond4==0, absorb(block) cluster(_cd_pre_n)

lincom 1.treat#1.midterm
matrix fx3[5,1] = r(estimate)
matrix fx3[5,2]=r(lb)
matrix fx3[5,3]=r(ub)

areg _voted treat_unmatch##midterm if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond6==0, absorb(block) cluster(_cd_pre_n)

lincom -1.treat_unmatch#1.midterm
matrix fx3[6,1] = r(estimate)
matrix fx3[6,2]=r(lb)
matrix fx3[6,3]=r(ub)

coefplot matrix(fx3[,1]), ci((fx3[,2] fx3[,3])) xline(0) xtitle("Change in Turnout") coeflabels(r1="Misaligned/Competitive to All" r2= "Aligned/Competitive to All" r3="Misaligned to Aligned/Misaligned" r4="Aligned to Aligned/Misaligned" r5="Competitive to Competitive/Aligned" r6="Competitive to Competitive/Misaligned") scheme(s1mono)

graph export "out/fx3.pdf", replace


*Additional Robustness checks (Different cutoff for classifying partisan alignment, evaluation of party registration to classify districts, etc.)


use `analysis_file_2016_temp', clear

merge m:1 state_name _cd_ year using party_reg_pvi


gen district_comp_dc = -1 if (50+_dem_registration)<=40
replace district_comp_dc = 1 if (50+_dem_registration) >=60
replace district_comp_dc = 0 if _dem_registration !=. & district_comp_dc==.

gen treat_dc = reg_status == district_comp_dc
gen treat_unmatch_dc = (reg_status == -1*district_comp_dc)

areg _voted treat_dc i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & (dropcond2==0 | dropcond4==0), absorb(block) cluster(_cd_pre_n)
estimates store reg1
estadd local bfe "Yes"
estadd local styrfe "Yes"

areg _voted treat_unmatch_dc i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & (dropcond2b==0 | dropcond6 == 0), absorb(block) cluster(_cd_pre_n)
estimates store reg2
estadd local bfe "Yes"
estadd local styrfe "Yes"

areg _voted treat_dc i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond2==0, absorb(block) cluster(_cd_pre_n)
estimates store reg3
estadd local bfe "Yes"
estadd local styrfe "Yes"

areg _voted treat_dc i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond2b==0, absorb(block) cluster(_cd_pre_n)
estimates store reg4
estadd local bfe "Yes"
estadd local styrfe "Yes"

areg _voted treat_dc i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond4==0, absorb(block) cluster(_cd_pre_n)
estimates store reg5
estadd local bfe "Yes"
estadd local styrfe "Yes"

areg _voted treat_unmatch_dc i._st_yr_n if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond6==0, absorb(block) cluster(_cd_pre_n)
estimates store reg6
estadd local bfe "Yes"
estadd local styrfe "Yes"

esttab reg1 reg2 reg3 reg4 reg5 reg6 using "out/table_alt_cutoff.tex", order(treat) drop( *y*r* _cons) star(* 0.10 ** 0.05 *** 0.01) r2 se label compress nonotes nomtitles ///
mgroups("\shortstack{Misaligned/\\Competitive\\ to All}" "\shortstack{Aligned/\\Competitive to\\ All}" "\shortstack{Misaligned to\\Aligned/\\Misaligned}" "\shortstack{Aligned to\\Aligned/\\Misaligned}" "\shortstack{Competitive to\\Competitive/\\Aligned}" "\shortstack{Competitive to \\Competitive/\\Misaligned}", pattern(1 1 1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
addnotes("Robust standard errors are in parentheses." "* p $<$ 0.10, ** p $<$ 0.05, *** p $<$ 0.01" "The sample is comprised of contested general elections by a D and R candidate.") /// 
title("Block FE: Individual Regressions of Turnout on Partisan Alignment, Alternate Cutoff for Lopsided Districts") scalars("styrfe State-Year FEs" "bfe Block FEs") replace

estimates clear


*Effect of size of change in district composition
bysort anid (year): gen _d_pvi = abs(_pvi[1] - _pvi[3]) if inlist(year,2012,2014)
bysort anid (year): replace _d_pvi = abs(_pvi[1] - _pvi) if year==2016

gen treat_d_pvi = treat * _d_pvi
gen treat_unmatch_d_pvi = treat_unmatch * _d_pvi

matrix fx4 = J(6,3,0)

areg _voted treat treat_d_pvi if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & (dropcond2==0 | dropcond4==0), absorb(block) cluster(_cd_pre_n)

lincom treat_d_pvi
matrix fx4[1,1] = r(estimate)
matrix fx4[1,2]=r(lb)
matrix fx4[1,3]=r(ub)

areg _voted treat_unmatch_d_pvi if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & (dropcond2b==0 | dropcond6 == 0), absorb(block) cluster(_cd_pre_n)

lincom -treat_unmatch_d_pvi
matrix fx4[2,1] = r(estimate)
matrix fx4[2,2]=r(lb)
matrix fx4[2,3]=r(ub)

areg _voted treat_d_pvi if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond2==0, absorb(block) cluster(_cd_pre_n)

lincom treat_d_pvi
matrix fx4[3,1] = r(estimate)
matrix fx4[3,2]=r(lb)
matrix fx4[3,3]=r(ub)

areg _voted treat_d_pvi if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond2b==0, absorb(block) cluster(_cd_pre_n)

lincom treat_d_pvi
matrix fx4[4,1] = r(estimate)
matrix fx4[4,2]=r(lb)
matrix fx4[4,3]=r(ub)

areg _voted treat_d_pvi if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond4==0, absorb(block) cluster(_cd_pre_n)

lincom treat_d_pvi
matrix fx4[5,1] = r(estimate)
matrix fx4[5,2]=r(lb)
matrix fx4[5,3]=r(ub)

areg _voted treat_unmatch_d_pvi if !missing(block) & year >=2012 & bp_abs_pvi_sr2__all==1 & dropcond6==0, absorb(block) cluster(_cd_pre_n)

lincom -treat_unmatch_d_pvi
matrix fx4[6,1] = r(estimate)
matrix fx4[6,2]=r(lb)
matrix fx4[6,3]=r(ub)

coefplot matrix(fx4[,1]), ci((fx4[,2] fx4[,3])) xline(0) xtitle("Change in Turnout") coeflabels(r1="Misaligned/Competitive to All" r2= "Aligned/Competitive to All" r3="Misaligned to Aligned/Misaligned" r4="Aligned to Aligned/Misaligned" r5="Competitive to Competitive/Aligned" r6="Competitive to Competitive/Misaligned") scheme(s1mono)

graph export "out/fx4.pdf", replace

